package com.liangzhong.model.friend
{
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.friend.vo.FriendHistoryVo;
	import com.liangzhong.model.friend.vo.FriendVo;
	import com.liangzhong.model.match.vo.MatchResultVo;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;

	/**
	 *好友关系管理 
	 * @author lake
	 * 
	 */	
	public class FriendManager extends EventDispatcher
	{
		/**
		 *好友列表 
		 */		
		private var _friendList :Array ;
		/**
		 *临时vo 其他地方查看 
		 */		
		public var tempFriendVo :FriendVo;
		/**
		 *受到邀请列表 
		 */		
		public var inviteList	:Array;
		/**
		 *推荐好友列表 
		 */
		public var recommendList:Array;
		/**
		 *受到邀请的数量 
		 */
		public var invitedfriend:int;
		
		/**
		 *请求数据时间 
		 */
		public var checkTime:int = 0;
		
		public function FriendManager()
		{
		}
		/**
		 * 解析
		 * @param dataList
		 * 
		 */		
		public function parseFriendList(dataList :Array):void
		{
			_friendList = [];
			var friendVo :FriendVo ;
			for each(var data :Object in dataList)
			{
				friendVo = new FriendVo();
				friendVo.analysis(data);
				if(friendVo.friendid <= 0)
				{
					continue;
				}
				_friendList.push(friendVo);
			}
			sortFriendList();
			//_friendList.sortOn(["offline","level"/*,"grade"*/],[Array.NUMERIC,Array.NUMERIC/*,Array.NUMERIC | Array.DESCENDING*/]);
			//test
			/*var i :int = 0;
			while(i < 25)
			{
				friendVo = new FriendVo();
				friendVo.cid = 100 + i;
				friendVo.grade = i + 1;
				friendVo.name = "我心飞扬" + i;
				friendVo.headurl2 = "http://pyapp.qlogo.cn/campus/69f677422f76c308b89433fac3111219001c6781850a5a7698ff100d07f63d2090561e579d43ca17/50";
				_friendList.push(friendVo);
				i ++ ;
			}*/
//			_friendList.push(null);
		}
		/**
		 *get  friendList
		 * @return 
		 */		
		public function get friendList():Array
		{
			return _friendList;
		}
		/**
		 *添加一个朋友 
		 * @param friendVo
		 */		
		public function addFriend(friendVo :FriendVo):void
		{
			_friendList.unshift(friendVo);
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		/**
		 * 对好友的排列 
		 * 
		 */
		public function sortFriendList():void
		{
			_friendList.sortOn(["offline","level"/*,"grade"*/],[Array.NUMERIC,Array.NUMERIC/*,Array.NUMERIC | Array.DESCENDING*/]);
		}
		/**
		 *获取朋友vo 通过id 
		 * @param id
		 * @return 
		 * 
		 */		
		public function getFriendById(id :int):FriendVo
		{
			for each(var fvo:FriendVo in _friendList)
			{
				if(fvo && fvo.friendid == id)
				{
					return fvo;
				}
			}
			return null;
		}
		/**
		 *通过好友ID判断是否为好友 
		 * @param id
		 * @return 
		 * 
		 */
		public function getisFrined(id:int):Boolean
		{
			if(_friendList == null)return true;
			for each(var fvo:FriendVo in _friendList)
			{
				if(fvo && fvo.friendid == id)
				{
					return true;
				}
			}
			return false;
		}
		/**
		 *通过id删除好友 
		 * @param id
		 * 
		 */		
		public function deleteFriendById(id :int):void
		{
			for each(var fvo:FriendVo in _friendList)
			{
				if(fvo && fvo.friendid == id)
				{
					_friendList.splice(_friendList.indexOf(fvo),1);
					this.dispatchEvent(new Event(Event.CHANGE));
					break;
				}
			}
		}
		
		/**
		 *好友比赛结果 
		 * @param $vo
		 * 
		 */
		public function friendMatchResult($vo:MatchResultVo):void
		{
			var fVo:FriendVo = getFriendById($vo.gcid);
			
			if(fVo == null)return;
				 
		}
			
		/**
		 *解析战绩 
		 * @param sList
		 * 
		 */		
		public function parseScoreList(sList :Array):Array
		{
			var rlist :Array = [];
			var fHistoryVo :FriendHistoryVo;
			var fvo :FriendVo;
			var uid :int  = AppData.getInstance().user.uid;
			for each(var item :Object in sList)
			{
				fHistoryVo = new FriendHistoryVo();
				fHistoryVo.analysis(item);
				if(fHistoryVo.gcid == uid)
				{
					fHistoryVo.gname = AppData.getInstance().user.userDetailInfo.nickname;
					fHistoryVo.gurl = AppData.getInstance().user.userDetailInfo.headurl;
					fvo = getFriendById(fHistoryVo.hcid);
					if(fvo == null)
					{
						fvo = tempFriendVo;
					}
					if(fvo)
					{
//						fHistoryVo.hname = fvo.name;
//						fHistoryVo.hurl = fvo.headurl2;
					}
					else
					{
						fHistoryVo.hname = "";
						fHistoryVo.hurl = "";
					}
				}
				else
				{
					fHistoryVo.hname = AppData.getInstance().user.userDetailInfo.nickname;
					fHistoryVo.hurl = AppData.getInstance().user.userDetailInfo.headurl;
					fvo = getFriendById(fHistoryVo.gcid);
					if(fvo == null)
					{
						fvo = tempFriendVo;
					}
					if(fvo)
					{
//						fHistoryVo.gname = fvo.name;
//						fHistoryVo.gurl = fvo.headurl2;
					}
					else
					{
						fHistoryVo.gname = "";
						fHistoryVo.gurl = "";
					}
				}
				rlist.push(fHistoryVo);
				rlist.sortOn("created",Array.NUMERIC | Array.DESCENDING);
			}
			return rlist;
		}
		/**
		 *比赛结果 
		 * @param value
		 * 
		 */		
		public function matchResult(value :MatchResultVo):void
		{
			if(_friendList == null)return;
			var fvo :FriendVo = getFriendById(value.gcid);
			if(fvo == null)return;
//			fvo.updateResult(value);
			fvo.matchResult(value);
			
			
		}
		
		
		/**
		 *解析邀请VO 
		 * 
		 */
		public function analysisInvoite($data:Object):void
		{
			if(inviteList == null)inviteList = [];
			inviteList.splice(0,inviteList.length);
			var arr:Array = $data.addfriendinfo;
			var len:int = arr.length;
			var vo:FriendVo;
			for(var i:int = 0;i < len;i++)
			{
				vo = new FriendVo();
				vo.analysis(arr[i]);
				inviteList.push(vo);
			}
		}
		/**
		 *有邀请好友请求过来 
		 * @param value
		 * 
		 */		
		public function addInviteVo(value :FriendVo):void
		{
			if(getInviteVoById(value.friendid))
			{
				return;
			}
			if(inviteList == null) inviteList = [];
			inviteList.unshift(value);
//			this.dispatchEvent(new FriendEvent(FriendEvent.RECEIVE_INVITE_INFO));
		}
		/**
		 *删除邀请信息 
		 * @param value
		 * 
		 */		
		public function deleteInviteVo(value :FriendVo):void
		{
			if(inviteList.indexOf(value)!=-1)
			{
				inviteList.splice(inviteList.indexOf(value),1);
//				this.dispatchEvent(new FriendEvent(FriendEvent.RECEIVE_INVITE_INFO));
			}
		}
		/**
		 *改变邀请数量 
		 * 
		 */
		public function changeinviteNumber($num:int):void
		{
			invitedfriend += $num;
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.FRIEND_DATA_CHANGE,invitedfriend);
		}
		/**
		 *获取邀请信息vo 通过id 
		 * @param id
		 * @return 
		 * 
		 */		
		public function getInviteVoById(id :int):FriendVo
		{
			for each(var vo:FriendVo in inviteList)
			{
				if(vo && vo.friendid == id)
				{
					return vo;
				}
			}
			return null;
		}
		/**
		 *根据id删除邀请 vo 
		 * @param $id
		 * 
		 */
		public function deleteInviteVoById($id:int):void
		{
			var len:int = inviteList.length;
			var vo:FriendVo
			for(var i:int = 0;i < len;i++)
			{
				vo = inviteList[i];
				if(vo != null && vo.friendid == $id)
				{
					inviteList.splice(i,1);
					break;
				}
			}
		}
		
		/***********推荐好友列表************************/
		/**
		 *更新推荐好友列表 
		 * 
		 */
		public function updataRecommendList(buffer:ByteArray):void
		{
			if(recommendList == null)recommendList = [];
			recommendList.splice(0,recommendList.length);//每次进来需要清除已经保存的数据
			var len:int = buffer.readShort();
			var vo:FriendVo;
			for(var i:int = 0;i < len;i++)
			{
				vo = new FriendVo();
				vo.analysisrecommend(buffer);
				recommendList.push(vo);
			}
		}
		/**
		 *删除推荐好友列表的成员 
		 * 
		 */
		public function deleteRecommendmen($id:int):void
		{
			if(recommendList == null)recommendList = [];
			var len:int = recommendList.length;
			var friendvo:FriendVo 
			for(var i:int = 0 ;i < len;i++)
			{
				friendvo = recommendList[i];
				if(friendvo.friendid == $id)
				{
					recommendList.splice(i,1);
					break;
				}
			}
		}
		
		
		/*********************************************/
		
	}
}